import { autocompletion, CompletionContext } from "@codemirror/autocomplete"
import { localCompletionSource, javascriptLanguage } from "@codemirror/lang-javascript"
import jsKeywords from './js-keywords.json'
export namespace TextEditorConfig {
    export function getEmptyTemplate(code?: string) {
        return `
        /**
         * script dog
        **/
        async function main() {
            ${code || "console.log('hello script dog')"}
        }
        `
    }
    function customCompletions(context: CompletionContext) {
        const word = context.matchBefore(/^\w+/)
        if (!word) {
            return null
        }
        if (word.from == word.to && !context.explicit)
            return null
        return {
            from: word.from,
            options: [
                ...jsKeywords,
                ...getGlobalVariables()
            ]
        }
    }

    function dotCompletions(context: CompletionContext) {
        const word = context.matchBefore(/\w+\.\w*/)
        if (!word) {
            return null
        }
        if (word.from == word.to && !context.explicit) {
            return null
        }
        const key = word.text.split('.')[0]
        const gvar = Reflect.get(window, key)
        if (gvar) {

            return {
                from: word.from + key.length + 1,
                to: word.to,
                options: getGlobalVariables(gvar)
            }
        }
        return null
    }

    function getGlobalVariables(obj: any = window) {
        const ar: any[] = []
        if (!obj) {
            return ar
        }
        const temp = new Set(Object.getOwnPropertyNames(obj))
        for (const k in obj) {
            temp.add(k)
        }
        for (const k of temp) {
            if (typeof k === 'symbol') {
                continue
            }
            if (k.startsWith('_')) {
                continue
            }
            const val = Reflect.get(obj, k)
            let kt = 'variable'
            switch(typeof val){
                case 'function':
                    kt = 'function'
                    break
                case "string":
                    kt = 'text'
                    break
                case "number":
                    kt = 'type'
                    break
                case "bigint":
                    kt = 'type'
                    break
                case "boolean":
                    kt = 'keyword'
                    break
                case "symbol":
                    kt = 'type'
                    break
                case "undefined":
                    kt = 'type'
                    break
                case "object":
                    kt = 'interface'
                    break
            }
            if (obj !== window && kt === 'constant') {
                kt = 'property'
            }
            ar.push({
                label: String(k),
                type: String(kt),
                info: kt === 'function' ? String(val) : k
            })
        }
        return ar
    }


    export function getCustomCompletion() {
        javascriptLanguage.extension
        return autocompletion({
            override: [
                customCompletions,
                localCompletionSource,
                dotCompletions,
            ]
        })
    }
}